function hfile2 = fsbf_SaveAsSRF(hfile, srffilename, options)
% FSBF::SaveAsSRF  - convert a FreeSurfer surface into BVQX's SRF
%
% FORMAT:       srf = fsbf.SaveAsSRF(srffilename [, options]);
%
% Input fields:
%
%       srffilename SRF filename
%       options     if given must be 1x1 struct with optional fields
%        .MeshCenter   1x3 double, default: [128, 128, 128]
%        .ConvexRGBA   1x4 double, default: [0.333, 0.677, 0.980, 0.400]
%        .ConcaveRGBA  1x4 double, default: [0.100, 0.240, 0.333, 0.400]
%        .VertexColor  Vx4 double (see SRF file format)
%        .RefFile      char, referenced file
%
% Output fields:
%
%       srf         SRF object

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'fsbf') || ...
   ~ischar(srffilename) || ...
    isempty(srffilename)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end

% get sizes
bc = bvqxfile_getcont(hfile.L);
vcrd = bc.VertexCoordinate;
numv = size(vcrd, 1);

% check options
if nargin < 3 || ...
   ~isstruct(options) || ...
    numel(options) ~= 1
    options = struct;
end
if isfield(options, 'MeshCenter') && ...
    isa(options.MeshCenter, 'double') && ...
    numel(options.MeshCenter) == 3 && ...
    ~any(isinf(options.MeshCenter) | isnan(options.MeshCenter))
    mshc = options.MeshCenter(:)';
else
    mshc = [128, 128, 128];
end
if isfield(options, 'ConvexRGBA') && ...
    isa(options.ConvexRGBA, 'double') && ...
    numel(options.ConvexRGBA) == 4 && ...
    ~any(isinf(options.ConvexRGBA(:)) | isnan(options.ConvexRGBA(:)) | ...
         options.ConvexRGBA(:) < 0 | options.ConvexRGBA(:) > 1)
    cvxr = options.ConvexRGBA(:)';
else
    cvxr = [0.333, 0.677, 0.98, 0.4];
end
if isfield(options, 'ConcaveRGBA') && ...
    isa(options.ConcaveRGBA, 'double') && ...
    numel(options.ConcaveRGBA) == 4 && ...
    ~any(isinf(options.ConcaveRGBA(:)) | isnan(options.ConcaveRGBA(:)) | ...
         options.ConcaveRGBA(:) < 0 | options.ConcaveRGBA(:) > 1)
    ccvr = options.ConcaveRGBA(:)';
else
    ccvr = [0.1, 0.24, 0.333, 0.4];
end
if isfield(options, 'VertexColor') && ...
    isa(options.VertexColor, 'double') && ...
    size(options.VertexColor, 1) == numv && ...
    size(options.VertexColor, 2) == 4 && ...
    numel(options.VertexColor) == (4 * numv)
    vcol = options.VertexColor;
else
    vcol = zeros(numv, 4);
end
if isfield(options, 'RefFile') && ...
    ischar(options.RefFile) && ...
   ~isempty(options.RefFile)
    rfl = options.RefFile(:)';
else
    rfl = '<none>';
end

% create SRF file in memory
hfile2 = createsrf(vcrd, bc.TriangleVertex, struct('offset', mshc));

% set options
bc2 = bvqxfile_getcont(hfile2.L);
bc2.ConvexRGBA = cvxr;
bc2.ConcaveRGBA = ccvr;
bc2.VertexColor = vcol;
bc2.AutoLinkedMTC = rfl;
bvqxfile_setcont(hfile2.L, bc2);

% check file writability
try
    aft_SaveAs(hfile2, srffilename);
catch
    BVQXfile(0, 'clearobj', hfile2.L);
    warning( ...
        'BVQXfile:FileNotWritable', ...
        'File not writable: ''%s''.', ...
        srffilename ...
    );
end
